/**
 * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springblade.modules.certificate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.certificate.entity.Certificate;
import org.springblade.modules.certificate.vo.CertificateVO;
import org.springblade.modules.certificate.mapper.CertificateMapper;
import org.springblade.modules.certificate.service.ICertificateService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.modules.oss.entity.Attach;
import org.springblade.modules.oss.mapper.AttachMapper;
import org.springblade.modules.oss.service.IAttachService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 资质表 服务实现类
 *
 * @author Blade
 * @since 2024-11-22
 */
@Service
public class CertificateServiceImpl extends BaseServiceImpl<CertificateMapper, Certificate> implements ICertificateService {

	@Autowired
	private AttachMapper attachMapper;

	@Autowired
	private IAttachService attachService;

	@Override
	public IPage<CertificateVO> selectCertificatePage(IPage<CertificateVO> page, CertificateVO certificate) {
		return page.setRecords(baseMapper.selectCertificatePage(page, certificate));
	}


	@Override
	public boolean submit(Certificate certificate) {
		this.saveOrUpdate(certificate);
		if (StringUtils.isNotBlank(certificate.getImgUrl())) {
			List<Long> longList = Func.toLongList(certificate.getImgUrl());
			List<Attach> attachList = attachMapper.selectBatchIds(longList);
			attachList.forEach(attach -> attach.setCertificateId(certificate.getId()));
			attachService.updateBatchById(attachList);
		}
		return true;
	}

	@Override
	public boolean deleteInfo(List<Long> longList) {
		for (Long id : longList) {
			List<Attach> attachList = attachService.list(new LambdaQueryWrapper<Attach>().eq(Attach::getCertificateId, id));
			if (CollectionUtils.isNotEmpty(attachList)) {
				String collect = attachList.stream().map(Attach::getId).map(String::valueOf).collect(Collectors.joining(","));
				attachService.deleteFile(collect);
			}
		}
		return this.deleteLogic(longList);
	}
}
